#import "YZRegInfoViewController.h"

#import "JsonUtil.h"
#import "NetWorkManager.h"
#import "PopUIManager.h"
#import "PhoneNumberChecker.h"
#import "RequestServerAPI.h"


@interface YZRegInfoViewController()
    
@end

@implementation YZRegInfoViewController
@synthesize label_province,label_garden, contactNumberInput,contactPerson;

- (void)viewDidLoad {
    [super viewDidLoad];
    [contactNumberInput addTarget:self action:@selector(textFieldDidChanged:) forControlEvents:UIControlEventEditingChanged];
    contactNumberInput.delegate = self;
    contactPerson.delegate = self;
    prov = @"null";
    city = @"null";
    country = @"null";
    town = @"null";
    schoolNameType = @"null";
}

-(void)textFieldDidChanged:(UITextField *) textFiled{
    NSLog(@"did change..");
    if (textFiled == contactNumberInput) {
        if (textFiled.text.length > 11) {
            textFiled.text = [textFiled.text substringToIndex:11];
        }
    }
}

- (IBAction)backButtonClick:(id)sender {
    [self.navigationController popViewControllerAnimated:YES];
}

- (IBAction)finishButtonClick:(id)sender {
    PopUIManager* popMgr = [PopUIManager getInstanse];
    //1.检查信息填写是否完整
    if (label_province.text.length < 4 || label_garden.text.length < 1 ||contactPerson.text.length < 1 || contactNumberInput.text.length < 11) {
        [popMgr popAlertView:self title:@"提示" message:@"检查信息填写是否完整" cancelButtonTitle:@"确定"];
        return;
    }
    
    //2.检查手机号码是否合法
    if (![PhoneNumberChecker isValidNumber:contactNumberInput.text]) {
        [popMgr popAlertView:self title:@"提示" message:@"手机号码不合法" cancelButtonTitle:@"确定"];
        return;
    }
    
    //3.发送幼儿园申请到服务器
    [RequestServerAPI apiCommitApply:self schoolName:self.label_garden.text schoolType:schoolNameType contactPerson:self.contactPerson.text phoneNumber:self.contactNumberInput.text province:prov city:city country:country town:town];
//    [popMgr popAlertView:self title:@"提示" message:@"资料已经提交审核。" cancelButtonTitle:@"确定"];
    
}

- (IBAction)provinceClickDown:(id)sender {
    NSLog(@"click down");
    label_province.textColor = [UIColor blackColor];
}

- (IBAction)provinceTouchUpOutSide:(id)sender {
    NSLog(@"touch up out side");
    label_province.textColor = [UIColor grayColor];
}

- (IBAction)provinceTouchUpInSide:(id)sender {
    NSLog(@"Province click.");
    label_province.textColor = [UIColor grayColor];
    //还原重置
    prov = @"null";
    city = @"null";
    country = @"null";
    town = @"null";
    [RequestServerAPI apiGetAddressList:self];
}

- (IBAction)gardenTouchDown:(id)sender {
    label_garden.textColor = [UIColor blackColor];
}

- (IBAction)gardenTouchUpInside:(id)sender {
    label_garden.textColor = [UIColor grayColor];
    
    if (label_province.text.length < 4) {
        [[PopUIManager getInstanse]popAlertView:self title:@"提示" message:@"请先选择地址" cancelButtonTitle:@"确定"];
        return;
    }
    
    [RequestServerAPI apiGetSchoolInTown:self townName:town];
}

- (IBAction)gardenTouchUpOutside:(id)sender {
    label_garden.textColor = [UIColor grayColor];
}

-(BOOL)textFieldShouldReturn:(UITextField *)textField{
    [self.view endEditing:YES];
    return YES;
}

-(void)passAddress:(NSString *)addressString schoolId:(NSString*)schoolId{
    label_province.text = addressString;
    NSArray *arr = [schoolId componentsSeparatedByString:@"_"];
    int count = (int)arr.count;
    if (0<count) {
        prov = [arr objectAtIndex:0];
    }
    
    if (1<count) {
        city = [arr objectAtIndex:1];
    }
    
    if (2<count) {
        country = [arr objectAtIndex:2];
    }
    
    if (3<count) {
        town = [arr objectAtIndex:3];
    }
}

-(void)setSchoolName:(NSString *)schoolNameString setType:(NSString*)type{
    label_garden.text = schoolNameString;
    schoolNameType = type;
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end
